/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pack;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The class QuestionVoteProcess stores the data needed to operate a voting question. 
 * In other words, one question starts voting on the desktop side, the corresponding QuestionVoteProcess object must be instantiate on the server side.
 * They are one-to-one relationship
 * @author shuyi
 */
public class QuestionVoteProcess {

    public QuestionVoteProcess(Socket socket, ObjectInputStream in, ObjectOutputStream out) {
        this.socket = socket;
        this.in=in;
        this.out=out;
        this.ansMap = new HashMap<Integer, String>();
        this.ansStat = new HashMap<String, Integer>();
    }

    public void chooseAns(int user_id, String ans) {
        if (this.ansMap.containsKey(user_id)) { // the user has already voted once
            // the the answer and decrease coresponding statics
            
            // change the value to new answer and increase the coresponding statics
            
        } else { try {
                // the user has not voted yet
           // create the key, set the value and increate the coresponding statics
           
           //send command to desktop app to increase the number of voted people
           out.writeByte(Command.SET_VOTED_PEOPLE);
           out.writeInt(ansMap.size());
           out.flush();
            } catch (IOException ex) {
                Logger.getLogger(QuestionVoteProcess.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void closeSocket(){
        // clean up the socket resources
    }
    // instance fields
    private Map<Integer, String> ansMap; // the key is the user_id, the String indicates the answer choosed by the user
    private Map<String, Integer> ansStat; // the key is the answer, the int shows how many students has chosen this answer
    private Socket socket; // the socket which reaches the desktop app who started the vote, mainly used to update the number of voted people
    private ObjectInputStream in;
    private ObjectOutputStream out;
}
